#include"Stack.h"
void StackInit(Stack *p)
{
	p->a = NULL;
	p->caoacity = p->top = 0;
}

void StackPush(Stack *p, int data) 
{
	assert(p);
	int newcopacity = p->caoacity == 0 ? 2 : p->caoacity * 2;
	if (p->caoacity == p->top) 
	{
		STDataType *tmp = (STDataType *)realloc(p->a,sizeof(STDataType) * newcopacity * 2);

		if (tmp != NULL) 
		{
			p->a = tmp;
			p->caoacity = newcopacity;
		}
	}
	p->a[p->top] = data;
	p->top++;
}



void Stackpop(Stack *p) 
{
	assert(p);
	assert(!StackEmpty(p));
	p->top--;
}

STDataType StackTop(Stack *p) 
{
	assert(p);
	assert(!StackEmpty(p));
	return p->a[p->top - 1];
}

int StackSize(Stack *p) 
{
	assert(p);
	return p->top;
}

int StackEmpty(Stack *p) 
{
	assert(p);
	return p->top == 0;
}

void StackDestroy(Stack *p) 
{
	assert(p);
	free(p->a);
	p->a = NULL;
	p->caoacity = p->top = 0;
}